package routing;

import geocoding.CustomNodes;

import java.util.ArrayList;

import org.osmdroid.views.overlay.MyLocationOverlay;

import osm.OsmReader;
import android.location.Location;

public class Trip {
	ArrayList<Integer> tripList = new ArrayList<Integer>();
	ArrayList<Boolean> checkList = new ArrayList<Boolean>();
	private ArrayList<Segment> segments;
	private ArrayList<Integer> landmarks; 
	String TAG="URNav";
	
	public ArrayList<Integer> getTripList(){
		return tripList;
	}
	public void addTripItem(int ID){
		tripList.add(ID);
		
	}
	public int getTripItem(int Pos){
		return tripList.get(Pos);
	}
	
	public int getPosition(int ID){
		return tripList.indexOf(ID);
	}

	
	public void addCheckItem(boolean checked){
		checkList.add(checked);
	}
	public boolean getCheckItem(int p){
		return checkList.get(p);
	}
	public void setCheckItem(int Pos){
		checkList.set(Pos, true);
	}
	
	public boolean isItemInTrip(int ID){
		boolean isThere=false; 
		for (int i=0; i<tripList.size(); i++){
			if (ID==getTripItem(i)) {
				isThere=true;
			}
			
			
		}
	return isThere;	
	}
	
	public void showTrip(){
		for (int i=0; i<tripList.size(); i++){
//			Log.i(TAG, "yxc ItemPosition: "+ i);
//			Log.i(TAG, "yxc ItemName: "+ String.valueOf(getTripItem(i)));
			
		}
	}

	public int getNextNotSilent(CustomNodes cn, int currentNodePosition){
		
		int nextNotSilent=0; 
		
		for (int i=currentNodePosition; i<tripList.size()- currentNodePosition; i++){
			
//			Log.i(TAG, "getNextNoSilent: " + mcn.getOsmID(getTripItem(i)) + mcn.getSilent(getTripItem(i)));
			
			if (cn.getSilent(getTripItem(i)) == false ){
				nextNotSilent= getTripItem(i);
				break;
			}
		}
//		Log.i(TAG, "From Trip.java, nextNotSilent = "  + mcn.getOsmID(nextNotSilent));
		return nextNotSilent;
	
	}
	
	
	
//	Checks where on the Trip the user currently is. Seeks the two nodes that are closest to the user and returns the one that is "earlier" on the trip.
	public int getCurrentNode(CustomNodes cn, MyLocationOverlay myLocOverlay){
		
		double closestDist=9999;
		double secondClosestDist=9999;
		double lastSecondDist=9999;
		int closestNodePosition=0;
		int secondClosestNodePosition=0;
		int currentNodePosition=0;
		int frontPosition=0;
		int backPosition=0;
		
		Location thisLocation=myLocOverlay.getLastFix();
		for (int i=0; i<tripList.size();i++){
		
			Location checkLocation=new Location("x");

							
			checkLocation.setLatitude(cn.getDlati(getTripItem(i)));
			checkLocation.setLongitude(cn.getDlongi(getTripItem(i)));
						
			if (thisLocation.distanceTo(checkLocation)<closestDist){
				
				closestDist=thisLocation.distanceTo(checkLocation);
				closestNodePosition=i;
				
			}
			
			Location closestLocation = new Location ("x");
			closestLocation.setLatitude(cn.getDlati(getTripItem(closestNodePosition)));
			closestLocation.setLongitude(cn.getDlongi(getTripItem(closestNodePosition)));
			
//			Location secondClosestLocation = new Location ("x");
//			secondClosestLocation.setLatitude(mcn.getDlati(getTripItem(secondClosestNodePosition)));
//			secondClosestLocation.setLongitude(mcn.getDlongi(getTripItem(secondClosestNodePosition)));
			

			
			if ((thisLocation.distanceTo(checkLocation)<secondClosestDist) && (thisLocation.distanceTo(checkLocation)>closestDist)){
				
//				if((checkLocation.bearingTo(closestLocation) - checkLocation.bearingTo(secondClosestLocation)) > 90){
//				lastSecondDist=secondClosestDist;
				secondClosestDist=thisLocation.distanceTo(checkLocation);
				secondClosestNodePosition=i;
//				}
			}
			
			


//			Location closestLocation = new Location ("x");
//			closestLocation.setLatitude(mcn.getDlati(getTripItem(closestNodePosition)));
//			closestLocation.setLongitude(mcn.getDlongi(getTripItem(closestNodePosition)));
			
//			|| (thisLocation.distanceTo(closestLocation)>8)
			
			if ((closestNodePosition>secondClosestNodePosition)){
				currentNodePosition=secondClosestNodePosition;
				frontPosition=closestNodePosition;
				backPosition=secondClosestNodePosition;
			}
			else {
				currentNodePosition=closestNodePosition;
				frontPosition=secondClosestNodePosition;
				backPosition=closestNodePosition;
			}
			
	}
//		Log.i(TAG, "From Trip.java; currentNodeID: " + mcn.getOsmID(getTripItem(currentNodePosition)));
		
		

		
		Location frontLocation = new Location("x");
		frontLocation.setLatitude(cn.getDlati(getTripItem(frontPosition)));
		
		
		if(frontLocation.distanceTo(thisLocation)>8){
			currentNodePosition=backPosition;
//			Log.i(TAG, "Back: " + mcn.getOsmID(getTripItem(backPosition)));
//			Log.i(TAG, "Front: " + mcn.getOsmID(getTripItem(frontPosition)));
		}
		
		return currentNodePosition;	
	}
	
	public int getCurrentNode2 (CustomNodes cn, MyLocationOverlay myLocOverlay){
		
		float closestDist=9999;
		int closestNodePosition=0;
		int currentNodePosition=0;
		Location thisLocation=myLocOverlay.getLastFix();
		
		for (int i=0; i<tripList.size();i++){
			Location checkLocation=new Location("x");

			checkLocation.setLatitude(cn.getDlati(getTripItem(i)));
			checkLocation.setLongitude(cn.getDlongi(getTripItem(i)));
			
			if (thisLocation.distanceTo(checkLocation)<closestDist){
				closestDist=thisLocation.distanceTo(checkLocation);
				closestNodePosition=i;
			}
			
		}
		
		Location closestNodeLocation = new Location ("x");
		closestNodeLocation.setLatitude(cn.getDlati(getTripItem(closestNodePosition)));
		closestNodeLocation.setLongitude(cn.getDlongi(getTripItem(closestNodePosition)));
		Location backNodeLocation = new Location ("x");
		backNodeLocation.setLatitude(cn.getDlati(getTripItem(closestNodePosition-1)));
		backNodeLocation.setLongitude(cn.getDlongi(getTripItem(closestNodePosition-1)));
		
		if (thisLocation.distanceTo(closestNodeLocation)>thisLocation.distanceTo(backNodeLocation)) {
			currentNodePosition= closestNodePosition-1;
		}
		
		else {
			currentNodePosition= closestNodePosition;
		}
		return currentNodePosition;
		
	}
	
	public int getCurrentNode3 (){
		
		int lastChecked=0; 
		for (int i=0; i<tripList.size(); i++){
			if (getCheckItem(i) == true){
				lastChecked=i;
			}
		
		}
		return lastChecked;
	}
	public ArrayList<Segment> getSegments() {
		return segments;
	}
	public void setSegments(ArrayList<Segment> segments) {
		this.segments = segments;
	}
	public ArrayList<Integer> getLandmarks() {
		return landmarks;
	}
	public void setLandmarks(ArrayList<Integer> landmarks) {
		this.landmarks = landmarks;
	}
	

}
